/*
 * @lc app=leetcode.cn id=150 lang=cpp
 *
 * [150] 逆波兰表达式求值
 */
#include <iostream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

// @lc code=start
class Solution
{
  private:
    int stringtoint(const string s)
    {
        int result = 0;
        for (auto ch : s) {
            if (ch == '-') {
                continue;
            }
            result = result * 10 + ch - '0';
        }
        return *(s.cbegin()) == '-' ? -result : result;
    }

  public:
    int evalRPN(vector<string> &tokens)
    {
        unordered_map<string, char> flags;
        stack<int> result;
        flags["+"] = '+';
        flags["-"] = '-';
        flags["*"] = '*';
        flags["/"] = '/';
        for (auto token : tokens) {
            if (flags.find(token) != flags.end()) {
                int new_num = 0;
                int num1 = result.top();
                result.pop();
                int num2 = result.top();
                result.pop();
                switch (flags[token]) {
                case '+':
                    new_num = num1 + num2;
                    break;
                case '-':
                    new_num = num2 - num1;
                    break;
                case '*':
                    new_num = num1 * num2;
                    break;
                case '/':
                    new_num = num2 / num1;
                    break;
                }
                result.push(new_num);
            } else {
                result.push(stringtoint(token));
            }
        }
        return result.top();
    }
};
// @lc code=end

int main()
{
    Solution sol;
    vector<string> str = {"3", "-4", "+"};
    cout << sol.evalRPN(str);
}
